<template>
    <div>
        <el-form ref="searchForm" :inline="true" :model="searchMap" style="margin-top:20px">
      <!-- 对于需要重置的字段需要加上prop=XX属性 -->
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="供应商名称"></el-input>
      </el-form-item>
        <el-form-item prop="linkman">
        <el-input v-if="!isDialog" v-model="searchMap.linkman" placeholder="联系人"></el-input>
      </el-form-item>
        <el-form-item prop="mobile">
        <el-input v-if="!isDialog" v-model="searchMap.mobile" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="fetchSupplier">查询</el-button>
        <el-button v-if="!isDialog" type="primary"  @click="toAdd">新增</el-button>
        <el-button v-if="!isDialog"  @click="resetForm('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>
        <el-table border height="400" :data="list" style="width: 100%">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="name" label="供应商"> </el-table-column>
      <el-table-column prop="linkman" label="联系人" width="90px"> </el-table-column>
      <el-table-column  v-if="!isDialog" prop="mobile" label="电话"> </el-table-column>
      <el-table-column  v-if="!isDialog" prop="remark" label="备注"> </el-table-column>
         <el-table-column v-if="isDialog" label="操作" width="150px">
        <template slot-scope="scope">
          <el-button size="mini" @click="MyhandleEdit(scope.row.id)"
            >选择</el-button
          >
        </template>
      </el-table-column>
      <el-table-column v-if="!isDialog" label="操作" width="150px">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
    :layout="!isDialog ? 'total, sizes, prev, pager, next, jumper' :'prev, pager, next'"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="新增会员" :visible.sync="dialogFormVisible" width="500px">
  <el-form :rules="rule" ref="SupplierForm" label-width="100px" label-position="right" :model="supplier" style="width:400px">
    <el-form-item label="供应商姓名" prop="name" >
      <el-input v-model="supplier.name" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="联系人" prop="linkman" >
      <el-input v-model="supplier.linkman" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="电话" prop="mobile" >
      <el-input v-model="supplier.mobile" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="备注" prop="remark" >
      <el-input v-model="supplier.remark" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="supplier.id === null ? addSupplier('SupplierForm') : edit('SupplierForm')">确 定</el-button>
  </div>
</el-dialog>
    </div>
</template>

<script>
import Supplier from '../api/supplier'
export default {
    //接收父组件传来的数据进行条件显示
    //如果定义了而父组件没有进行传值，那么就为Boolean
    props:{
        isDialog:Boolean,
    },
    data() {
        return {
            list:[],
            currentPage:1,
            pageSize:10,
            total:0,
            searchMap:{
                name:"",
                linkman:"",
                mobile:"",
            },
            supplier:{
                id:null,
                name:"",
                linkman:"",
                mobile:"",
                remark:"",
            },
            dialogFormVisible:false,
            rule:{
                name:[
                    {required:true ,message:"请输入供应商姓名" , trigger:"blur"},
                ],
                linkman:[
                    {required:true ,message:"请输入供应商姓名" , trigger:"blur"},
                ],
                mobile:[
                    {required:true ,message:"请输入供应商姓名" , trigger:"blur"},
                ]
            }
        }
    },
    methods: {
        //单击每个供应商向父组件传值
        MyhandleEdit(id){
            Supplier.getOne(id).then(res=>{
                const result = res.data;
                if(result.flag){
                    this.supplier = result.data;
                    //触发父组件自定义事件进行传值
                    this.$emit("option-supplier",this.supplier);
                    console.log(this.supplier);
                }
            })
        },
        // handleCurrentChange(currentRow){
        //     console.log(currentRow);
        // },
        //添加供应商
        addSupplier(formName){
            this.$refs[formName].validate(valid=>{
                //如果校验成功就添加
                if(valid){
                    Supplier.addSupplier(this.supplier).then(res=>{
                        const result = res.data;
                        if(result.flag){
                            this.dialogFormVisible = false;
                            this.$message({
                                message:result.msg,
                                type:"success",
                            });
                        }else{
                                this.$message({
                                message:result.msg,
                                type:"error",
                            });
                        }
                    })
                }else{
                    return false;
                }
            })
        },
        toAdd(){
            this.dialogFormVisible = true;
            this.$nextTick(()=>{
                this.$refs['SupplierForm'].resetFields();
            })
        },
        //获取供应商列表
        fetchSupplier(){
            // Supplier.getList().then(res=>{
                Supplier.search(this.currentPage,this.pageSize,this.searchMap).then(res=>{
                const result = res.data;
                this.total = result.data.total;
                if(result.flag){
                    this.list = result.data.rows;
                    console.log(this.list);
                }
            })
        },
        //编辑回显函数
        handleEdit(id){
            console.log(id);
            this.toAdd();
            Supplier.getOne(id).then(res=>{
                const result = res.data;
                if(result.flag){
                    this.supplier = result.data;
                }
            })
        },
        //编辑供应商
        edit(formName){
            this.$refs[formName].validate(valid=>{
                //编辑信息校验成功就修改
                if(valid){
                    Supplier.editOne(this.supplier).then(res=>{
                        const result = res.data;
                        if(result.flag){
                            //修改之后回显数据
                            this.fetchSupplier();
                            this.dialogFormVisible = false;
                            this.$message({
                                message:result.msg,
                                type:"success",
                            });
                        }else{
                            this.$message({
                                message:result.msg,
                                type:"error",
                            });
                        }
                    })
                }else{
                    return false;
                }
            })
        },
        //删除函数
        handleDelete(id){
            console.log(id);
            this.$confirm("此操作将删除会员信息, 是否继续?","提示",{
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(()=>{
                //点击确定以后进行删除
                Supplier.deleteOne(id).then(res=>{
                    const result = res.data;
                    if(result.flag){
                        this.$message({
                            message:result.msg,
                            type:"success",
                        });
                        this.fetchSupplier();
                    }else{
                            this.$message({
                            message:result.msg,
                            type:"error",
                        });
                    }
                })
            }).catch(()=>{
                //取消以后不删除
            })
        },
        //每页条数变化函数
        handleSizeChange(val){
            this.pageSize = val;
            this.fetchSupplier();
        },
        //页码变化时函数
        handleCurrentChange(val){
            this.currentPage = val;
            this.fetchSupplier();
        },
        //搜索重置
        resetForm(formName){
            this.$refs[formName].resetFields();
        },
    },
    created() {
        this.fetchSupplier();
    },
}
</script>